#include "Cannyble.h"
#include "Player.h"
#include "Runner.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "../SGD Wrappers/SGD_EventManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"


CCannyble::CCannyble()
{
	Listener::RegisterForEvent("ASSESS_THREAT");
	Listener::RegisterForEvent("TORNADO_SWIRL");
	Listener::RegisterForEvent("NO_AGGRO");
	m_fDamage = 10;
	m_fAttackTimer = 2;
	m_fDefAttack = 2;
}


CCannyble::~CCannyble()
{
	Listener::UnregisterFromEvent("ASSESS_THREAT");
	Listener::UnregisterFromEvent("TORNADO_SWIRL");
	Listener::UnregisterFromEvent("NO_AGGRO");
}

void CCannyble::Update(float elapsedTime)
{
	if (m_bExhausted == true)
	{
		m_fRespawnTimer -= elapsedTime;
		m_bIsActive = false;
		m_ptPosition = m_ptHomePos;
		m_CurrState = IDLE_STATE;
	}
	else
	{
		if (SGD::InputManager::GetInstance()->IsKeyDown(SGD::Key::W))
		{
			m_fCurrStamina = m_fMaxStamina * 0.5f;
		}

		if (SGD::InputManager::GetInstance()->IsKeyDown(SGD::Key::Q))
			m_fCurrStamina = 0;

		if (m_tTable.Target != nullptr)
		{
			if (m_tTable.Target != nullptr)
				SwitchStates(CAI::CHASE_STATE);


			if (m_CurrState == CHASE_STATE && m_bInRange == true)
			{
				SwitchStates(ATTACK_STATE);
			}
		}

		if (m_fCurrStamina / m_fMaxStamina <= 0.5)
			SwitchStates(RETURN_STATE);
	}

	if (m_fRespawnTimer <= 0)
	{
		m_bExhausted = false;
		m_bIsActive = true;
		m_fCurrStamina = m_fMaxStamina;
		m_tTable.Target = nullptr;
		m_CurrState = IDLE_STATE;
		m_vtVelocity = { 0, 0 };
		m_fRespawnTimer = 5;
	}


	if (m_tTable.Target != nullptr && m_fNoAggro > 0 && m_pSafeTarget != nullptr)
	{
		bool tempSafeTeam = m_pSafeTarget->GetTeam();
		if (tempSafeTeam == m_bSafeTeam)
		{
			m_tTable.Target = nullptr;
			SwitchStates(RETURN_STATE);
		}
	}

	m_fNoAggro -= elapsedTime;
	CAI::Update(elapsedTime);
}

void CCannyble::HandleEvent(const SGD::Event* pEvent) /*override*/
{
	SGD::Vector attackTarget = reinterpret_cast<CPlayer*>(pEvent->GetSender())->GetPosition() - m_ptPosition;

	if (pEvent->GetEventID() == "ASSESS_THREAT")
	{
		if (attackTarget.ComputeLength() <= 100)
			m_tTable.Target = reinterpret_cast<CPlayer*>(pEvent->GetSender());
	}

	if (pEvent->GetEventID() == "TORNADO_SWIRL")
	{
		if (pEvent->GetSender() != this)
		{

			switch (reinterpret_cast<int>(pEvent->GetData()))
			{
			case 1:
			{
					  //check how far I am so I can get effected by the tornado
					  if (SGD::Vector(reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos() - this->GetPosition()).ComputeLength() < 100)
						  this->RotatePlayers(this, 0.005f*2.0f, reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos());
			}
				break;
			case 2:
			{
					  //check how far I am so I can get effected by the tornado
					  if (SGD::Vector(reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos() - this->GetPosition()).ComputeLength() < 150)
						  this->RotatePlayers(this, 0.007f*2.0f, reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos());
			}
				break;
			case 3:
			{
					  //check how far I am so I can get effected by the tornado
					  if (SGD::Vector(reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos() - this->GetPosition()).ComputeLength() < 250)
						  this->RotatePlayers(this, 0.01f*2.0f, reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos());
			}
				break;
			default:
				break;
			}
		}
	}

	if (pEvent->GetEventID() == "NO_AGGRO")
	{
		m_fNoAggro = 15;
		m_pSafeTarget = reinterpret_cast<CPlayer*>(pEvent->GetSender());
		m_bSafeTeam = m_pSafeTarget->GetTeam();
	}

	Entity::HandleEvent(pEvent);
}